package com.pzj.swtcallcom.ocx;

import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleControlSite;
import org.eclipse.swt.ole.win32.OleListener;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Composite;

public class SuperOcx extends Composite {
	protected OleAutomation player;
	protected OleControlSite controlSite;
	protected int Type;

	/**
	 * @param parent
	 * @param style
	 */
	public SuperOcx(Composite parent, int style) {
		super(parent, style);
	}

	protected int getType() {
		return Type;
	}

	protected void setType(int type) {
		Type = type;
	}

	public int getID(String name) {
		try {
			int[] ids = player.getIDsOfNames(new String[] { name });
			if (ids.length >= 0)
				return ids[0];
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return -1;
	}

	public int[] getIDs(String... name) {
		try {
			int[] ids = player.getIDsOfNames(name);
			if (ids.length >= 0)
				return ids;
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return null;
	}

	public Variant execute(String methodName) {
		int mid = getID(methodName);
		if (mid < 0)
			return null;

		Variant rtnv = player.invoke(mid);
		return rtnv;
	}

	public Variant getProperty(String parameter) {
		int mid = getID(parameter);
		if (mid < 0)
			return null;
		return player.getProperty(mid);
	}

	public Variant execute(String methodName, Variant[] parameter) {
		int mid = getID(methodName);
		if (mid < 0)
			return null;

		Variant rtnv = player.invoke(mid, parameter);
		return rtnv;
	}

	public Variant executes(String[] methodName, Variant[] parameter) {
		int[] mid = getIDs(methodName);
		if (mid == null)
			return null;
		int[] x = new int[1];
		x[0] = mid[1];
		Variant rtnv = player.invoke(mid[0], parameter, x);
		return rtnv;
	}

	/**
	 * 添加事件监听
	 * 
	 * @param eventID
	 * @param listener
	 */
	public void addEventListener(int eventID, OleListener listener) {
		controlSite.addEventListener(eventID, listener);
	}

	/**
	 * 移除事件监听
	 * 
	 * @param eventID
	 * @param listener
	 */
	public void removeEventListener(int eventID, OleListener listener) {

		controlSite.removeEventListener(eventID, listener);
	}
}
